

export function data2FormData(data) {
  if (Object.prototype.toString.call(data) !== '[object Object]') return

  const formData = new FormData()

  Object.keys(data).forEach(key => {
    const value = data[key]
    if (Array.isArray(value)) {
      value.forEach(item => {
        formData.append(key, item)
      })
    } else {
      formData.append(key, value)
    }

  })

  return formData
}

export function removeObjNullValue(object) {

  if (Object.prototype.toString.call(object) !== '[object Object]') {
    throw new Error('removeObjBlankVal params is not object')
  }

  const newObj = {}
  for (const key in object) {
    if (Object.hasOwnProperty.call(object, key)) {
      const value = object[key];
      if (value || value === 0) {
        newObj[key] = value
      }
    }
  }
  return newObj
}

/**
 * 
 * @param {Object} obj 
 */

export function obj2Url(obj) {
  let url = ''

  for (const key in obj) {
    if (Object.hasOwnProperty.call(obj, key)) {
      const value = obj[key];

      url += `${key}=${encodeURIComponent(value)}&`

    }
  }

  return url.substring(0, url.length - 1)
}


export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

export function getTotalPageNum(totalRecord, pageSize) {
  return parseInt((totalRecord + pageSize - 1) / pageSize);
}

export function addAccessToken(paramsObj = {}) {
  const accountInfo = uni.getStorageSync('accountInfo') || {}
  paramsObj.AccessToken = accountInfo.accessToken || ''
  return paramsObj
}

export function dataURIToBlob(dataURI) {
  console.log('dataURIToBlob', dataURI)
  const splitDataURI = dataURI.split(',')
  const byteString = splitDataURI[0].indexOf('base64') >= 0 ? atob(splitDataURI[1]) : decodeURI(splitDataURI[1])
  const mimeString = splitDataURI[0].split(':')[1].split(';')[0]

  const ia = new Uint8Array(byteString.length)
  for (let i = 0; i < byteString.length; i++)
    ia[i] = byteString.charCodeAt(i)
  console.log('mimeString', mimeString)

  return new Blob([ia], { type: mimeString })
}

export function deepClone(target) {
  // 定义一个变量
  let result
  if (typeof target === 'object') { // 如果当前需要深拷贝的是一个对象的话
    // 如果是一个数组的话
    if (Array.isArray(target)) {
      result = [] // 将result赋值为一个数组，并且执行遍历
      for (let i in target) {
        // 递归克隆数组中的每一项
        result.push(deepClone(target[i]))
      }
    } else if (target === null) { // 判断如果当前的值是null的话，直接赋值为null
      result = null
    } else if (target.constructor === RegExp) { // 判断如果当前的值是一个RegExp对象的话，直接赋值
      result = target
    } else { // 否则是普通对象，直接for in循环，递归赋值对象的所有值
      result = {}
      for (let i in target) {
        result[i] = deepClone(target[i])
      }
    }
  } else { // 如果不是对象的话，就是基本数据类型，那么直接赋值
    result = target
  }
  // 返回最终结果
  return result
}
